/*=============================================================================
    jibits/html.js
    Copyright (C) 2008-2009, Donald W. Griffin
    All rights reserved.
    [MIT license :: see license.txt for details]
=============================================================================*/
$module("jibits.html");

$requires("zjs.ext");

$namespace ("jibits", function ()
{
    var pattern = "<script[^>]*>([\\S\\s]*?)<\\/script>";

    return (
    {
        kAllScriptsRegex : new RegExp(pattern, "img"),
        kOneScriptRegex : new RegExp(pattern, "im")
    });

}())

$namespace (jibits, {

kAllTagsRegex : /<\/?[^>]+>/gi,

extractScripts : function (str)
{
    var m = str.match(jibits.kAllScriptsRegex);
    var ret = [];
    if (m)
        for (var i = 0, n = m.length; i < n; ++i)
        {
            var s = m[i], m2 = s.match(jibits.kOneScriptRegex);
            if (m2)
                ret.push(m2[1]);
        }

    return ret;
},

scrubScripts : function (str)
{
    return str.replace(jibits.kAllScriptsRegex, "");
},

scrubTags : function (str)
{
    return str.replace(jibits.kAllTagsRegex, "");
},

escapeHtml : function (str)
{
    return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').
               replace(/>/g,'&gt;');
},

unescapeHtml : function (str)
{
    return jibits.scrubTags(str).replace(/&amp;/g,'&').
                  replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

})
